using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common;
using Common.Enum;
using Common.NotifyEvent;
using SProject;
using SProject.Paramter;
using SProject.Rule;

namespace SExperiment
{
    [Serializable]
    public class Subset : NotifyPropertyChanged
    {
        [Serializable]
        public class SubsetParamter : NotifyPropertyChanged
        {
            private BasicOption _BasicOption;

            private Rule _Rule;

            private CrossTalk _CrossTalk;

            private ProjectStdCurve _CurrentProjectStdCurve;

            private List<HRMRefTarget> _RefTarget = new List<HRMRefTarget>();

            public BasicOption BasicOption
            {
                get
                {
                    return _BasicOption;
                }
                set
                {
                    if (_BasicOption != value)
                    {
                        _BasicOption = value;
                        OnPropertyChanged("BasicOption");
                    }
                }
            }

            public Rule Rule
            {
                get
                {
                    return _Rule;
                }
                set
                {
                    if (_Rule != value)
                    {
                        _Rule = value;
                        OnPropertyChanged("Rule");
                    }
                }
            }

            public CrossTalk CrossTalk
            {
                get
                {
                    return _CrossTalk;
                }
                set
                {
                    if (_CrossTalk != value)
                    {
                        _CrossTalk = value;
                        OnPropertyChanged("CrossTalk");
                    }
                }
            }

            public ProjectStdCurve CurrentProjectStdCurve
            {
                get
                {
                    return _CurrentProjectStdCurve;
                }
                set
                {
                    if (_CurrentProjectStdCurve != value)
                    {
                        _CurrentProjectStdCurve = value;
                        OnPropertyChanged("CurrentProjectStdCurve");
                    }
                }
            }

            public List<HRMRefTarget> RefTarget
            {
                get
                {
                    return _RefTarget;
                }
                set
                {
                    if (_RefTarget != value)
                    {
                        _RefTarget = value;
                        OnPropertyChanged("RefTarget");
                    }
                }
            }
        }

        public class CurveParameter : NotifyPropertyChanged
        {
            private bool _Use;

            private int _WellNo;

            private int _ChannelNo;

            private int _BeginBaseline;

            private int _EndBaseline;

            private double _Threshold = 0.11999999731779099;

            public bool Use
            {
                get
                {
                    return _Use;
                }
                set
                {
                    if (_Use != value)
                    {
                        _Use = value;
                        OnPropertyChanged("Use");
                    }
                }
            }

            public int WellNo
            {
                get
                {
                    return _WellNo;
                }
                set
                {
                    if (_WellNo != value)
                    {
                        _WellNo = value;
                        OnPropertyChanged("WellNo");
                    }
                }
            }

            public int ChannelNo
            {
                get
                {
                    return _ChannelNo;
                }
                set
                {
                    if (_ChannelNo != value)
                    {
                        _ChannelNo = value;
                        OnPropertyChanged("ChannelNo");
                    }
                }
            }

            public int BeginBaseline
            {
                get
                {
                    return _BeginBaseline;
                }
                set
                {
                    if (_BeginBaseline != value)
                    {
                        _BeginBaseline = value;
                        OnPropertyChanged("BeginBaseline");
                    }
                }
            }

            public int EndBaseline
            {
                get
                {
                    return _EndBaseline;
                }
                set
                {
                    if (_EndBaseline != value)
                    {
                        _EndBaseline = value;
                        OnPropertyChanged("EndBaseline");
                    }
                }
            }

            public double Threshold
            {
                get
                {
                    return _Threshold;
                }
                set
                {
                    if (_Threshold != value)
                    {
                        _Threshold = value;
                        OnPropertyChanged("Threshold");
                    }
                }
            }

            public CurveParameter Clone()
            {
                CurveParameter curveParameter = new CurveParameter();
                curveParameter.Use = Use;
                curveParameter.WellNo = WellNo;
                curveParameter.ChannelNo = ChannelNo;
                curveParameter.BeginBaseline = BeginBaseline;
                curveParameter.EndBaseline = EndBaseline;
                curveParameter.Threshold = Threshold;
                return curveParameter;
            }

            public void SaveToStream(StreamEx stream)
            {
                stream.Write(PropertyKey.Key_CurveParameter);
                List<PropertyKey> list = new List<PropertyKey>();
                List<byte[]> list2 = new List<byte[]>();
                list.Add(PropertyKey.Key_Use);
                list2.Add(BytesConverter.GetBytes(Use));
                list.Add(PropertyKey.Key_WellNo);
                list2.Add(BytesConverter.GetBytes(WellNo));
                list.Add(PropertyKey.Key_ChannelNo);
                list2.Add(BytesConverter.GetBytes(ChannelNo));
                list.Add(PropertyKey.Key_BaginBaseLine);
                list2.Add(BytesConverter.GetBytes(BeginBaseline));
                list.Add(PropertyKey.Key_EndBaseLine);
                list2.Add(BytesConverter.GetBytes(EndBaseline));
                list.Add(PropertyKey.Key_Threshold);
                list2.Add(BytesConverter.GetBytes(Threshold));
                stream.Write(PropertyKey.Key_Index);
                stream.WriteIndex(list, list2);
                stream.Flush();
            }

            public bool ReadFromStream(StreamEx stream)
            {
                PropertyKey propertyKey = stream.ReadKey();
                if (propertyKey == PropertyKey.Key_CurveParameter)
                {
                    propertyKey = stream.ReadKey();
                    if (propertyKey == PropertyKey.Key_Index)
                    {
                        List<PropertyKey> keys;
                        List<byte[]> dates;
                        stream.ReadIndex(out keys, out dates);
                        for (int i = 0; i < keys.Count; i++)
                        {
                            switch (keys[i])
                            {
                                case PropertyKey.Key_Use:
                                    Use = BytesConverter.BytesToBool(dates[i]);
                                    break;
                                case PropertyKey.Key_WellNo:
                                    WellNo = BytesConverter.BytesToInt(dates[i]);
                                    break;
                                case PropertyKey.Key_ChannelNo:
                                    ChannelNo = BytesConverter.BytesToInt(dates[i]);
                                    break;
                                case PropertyKey.Key_BaginBaseLine:
                                    BeginBaseline = BytesConverter.BytesToInt(dates[i]);
                                    break;
                                case PropertyKey.Key_EndBaseLine:
                                    EndBaseline = BytesConverter.BytesToInt(dates[i]);
                                    break;
                                case PropertyKey.Key_Threshold:
                                    Threshold = BytesConverter.BytesToDouble(dates[i]);
                                    break;
                            }
                        }
                        return true;
                    }
                    stream.Seek(-4L, SeekOrigin.Current);
                    return false;
                }
                stream.Seek(-4L, SeekOrigin.Current);
                return false;
            }
        }

        private bool _IsReadOnly;

        private string _Name = "Subset";

        private List<Well> _Wells = new List<Well>();

        private Hashtable SubsetParamters = new Hashtable();

        public Hashtable CurveParameters = new Hashtable();

        private List<KeyValuePair> _GenoTypeList;

        public bool IsReadOnly
        {
            get
            {
                return _IsReadOnly;
            }
            set
            {
                if (_IsReadOnly != value)
                {
                    _IsReadOnly = value;
                    OnPropertyChanged("IsReadOnly");
                }
            }
        }

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                if (!(_Name == value))
                {
                    _Name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public List<Well> Wells
        {
            get
            {
                return _Wells;
            }
            set
            {
                if (_Wells != value)
                {
                    _Wells = value;
                    OnPropertyChanged("Wells");
                }
            }
        }

        public List<KeyValuePair> GenoTypeList
        {
            get
            {
                return _GenoTypeList;
            }
            set
            {
                if (_GenoTypeList != value)
                {
                    _GenoTypeList = value;
                    OnPropertyChanged("GenoTypeList");
                }
            }
        }

        public Subset()
        {
            GenoTypeList = new List<KeyValuePair>
            {
                new KeyValuePair
                {
                    Key = EGenoType.GenoBadWell,
                    Value = Utility.GetMessage("bad_well")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoNegative,
                    Value = Utility.GetMessage("Negative")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoUnknown,
                    Value = Utility.GetMessage("Geno_Unknown")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoType1,
                    Value = Utility.GetMessage("Geno_Type1")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoType2,
                    Value = Utility.GetMessage("Geno_Type2")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoType3,
                    Value = Utility.GetMessage("Geno_Type3")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoType4,
                    Value = Utility.GetMessage("Geno_Type4")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoType5,
                    Value = Utility.GetMessage("Geno_Type5")
                },
                new KeyValuePair
                {
                    Key = EGenoType.GenoType6,
                    Value = Utility.GetMessage("Geno_Type6")
                }
            };
        }

        public Subset(string name)
            : this()
        {
            Name = name;
        }

        public List<Sample> GetAllSample(Project prj)
        {
            return (from s in _Wells
                    where s.Sample != null && (prj == null || s.Project == prj)
                    select s.Sample).Distinct().ToList();
        }

        public List<Project> GetAllProject()
        {
            return (from s in _Wells
                    where s.Project != null
                    select s.Project).Distinct().ToList();
        }

        public SubsetParamter GetSubsetParamter(string projectName)
        {
            return GetSubsetParamter((from s in _Wells
                                      where s.Project != null && s.Project.BasicInfo.Name == projectName
                                      select s.Project).FirstOrDefault());
        }

        public void UpdateProject(Project oldPrj, Project newPrj)
        {
            if (SubsetParamters.Contains(oldPrj))
            {
                SubsetParamter value = SubsetParamters[oldPrj] as SubsetParamter;
                SubsetParamters.Remove(oldPrj);
                SubsetParamters.Add(newPrj, value);
            }
        }

        public SubsetParamter GetSubsetParamter(Project project)
        {
            SubsetParamter subsetParamter = null;
            if (SubsetParamters.Contains(project))
            {
                subsetParamter = SubsetParamters[project] as SubsetParamter;
            }
            else
            {
                if (_Wells.Where((Well s) => s.Project == project).Count() == 0)
                {
                    return null;
                }
                SubsetParamter subsetParamter2 = new SubsetParamter();
                subsetParamter2.BasicOption = OptionFactory.CreateBasicOption(project.BasicInfo.Type);
                subsetParamter2.CrossTalk = new CrossTalk(project.CrossTalk);
                subsetParamter2.Rule = project.Rule.Clone();
                subsetParamter = subsetParamter2;
                subsetParamter.BasicOption.Copy(project.BasicOption);
                SubsetParamters.Add(project, subsetParamter);
                if (project.BasicInfo.Type == EProjectType.HRM)
                {
                    foreach (BasicOptionItem item in project.BasicOption.Items)
                    {
                        subsetParamter.RefTarget.Add(new HRMRefTarget
                        {
                            TargetName = item.TargetName,
                            RefMode = ERefMode.Auto
                        });
                    }
                }
            }
            if (subsetParamter.CurrentProjectStdCurve == null)
            {
                subsetParamter.CurrentProjectStdCurve = new ProjectStdCurve();
            }
            subsetParamter.CurrentProjectStdCurve.ProjectName = project.BasicInfo.Name;
            return subsetParamter;
        }

        public List<Project> GetQuantitativeProjectList()
        {
            List<Project> list = new List<Project>();
            foreach (Project item in GetAllProject())
            {
                BasicOption basicOption = GetSubsetParamter(item).BasicOption;
                foreach (BasicOptionItem item2 in basicOption.Items)
                {
                    if (item2.AnalysisType == EAnalysisType.ANALYSIS_QUANTITATIVE)
                    {
                        list.Add(item);
                        break;
                    }
                }
            }
            return list;
        }

        public List<Project> GetQuantitativeStandartProjectList()
        {
            return (from s in _Wells
                    where s.Sample != null && s.Sample.Items.Where((SampleTargetItem s1) => s1.SampleType == SampleType.Standard).Count() > 0 && GetSubsetParamter(s.Project).BasicOption.Items.Where((BasicOptionItem s1) => s1.AnalysisType == EAnalysisType.ANALYSIS_QUANTITATIVE).Count() > 0
                    select s.Project).Distinct().ToList();
        }

        public void SaveStandardData(Project prj, string path, string version)
        {
            List<Well> list = _Wells.Where((Well s) => s.Project == prj && s.Sample.Items.Where((SampleTargetItem s1) => s1.SampleType == SampleType.Standard).Count() > 0).ToList();
            if (list.Count <= 0)
            {
                return;
            }
            ProjectStdCurve projectStdCurve = new ProjectStdCurve();
            projectStdCurve.ProjectName = prj.BasicInfo.Name;
            projectStdCurve.ExternalStdWell = list;
            projectStdCurve.ExternalTargetStdCurveList = GetSubsetParamter(list[0].Project).CurrentProjectStdCurve.TargetStdCurveList;
            projectStdCurve.Version = version;
            ProjectStdCurve projectStdCurve2 = projectStdCurve;
            FileStream fileStream = null;
            StreamEx streamEx = new StreamEx();
            try
            {
                projectStdCurve2.SaveToStream(streamEx);
                streamEx.Seek(0L, SeekOrigin.Begin);
                fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
                streamEx.CopyTo(fileStream);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (streamEx != null)
                {
                    streamEx.Close();
                }
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }

        public void SaveToStream(StreamEx stream, List<Well> allWell)
        {
            List<Project> allProject = (from s in allWell
                                        where s.Project != null
                                        select s.Project).Distinct().ToList();
            List<Project> list = (from Project s in SubsetParamters.Keys
                                  where allProject.IndexOf(s) == -1
                                  select s).ToList();
            foreach (Project item in list)
            {
                SubsetParamters.Remove(item);
            }
            stream.Write(PropertyKey.Key_Subset);
            List<PropertyKey> list2 = new List<PropertyKey>();
            List<byte[]> list3 = new List<byte[]>();
            StreamEx streamEx = null;
            if (!string.IsNullOrEmpty(Name))
            {
                list2.Add(PropertyKey.Key_Name);
                list3.Add(BytesConverter.GetBytes(Name));
            }
            list2.Add(PropertyKey.Key_Wells);
            streamEx = new StreamEx();
            streamEx.Write(Wells.Count);
            foreach (Well well in Wells)
            {
                streamEx.Write(allWell.IndexOf(well));
            }
            list3.Add(streamEx.ToArray());
            streamEx.Close();
            list2.Add(PropertyKey.Key_SubsetParamters);
            streamEx = new StreamEx();
            streamEx.Write(SubsetParamters.Count);
            foreach (Project key in SubsetParamters.Keys)
            {
                streamEx.Write(allProject.IndexOf(key));
                SubsetParamter param = SubsetParamters[key] as SubsetParamter;
                SaveToStreamSubsetParamter(param, streamEx);
            }
            list3.Add(streamEx.ToArray());
            streamEx.Close();
            if (CurveParameters.Count > 0)
            {
                list2.Add(PropertyKey.Key_CurveParameters);
                streamEx = new StreamEx();
                streamEx.Write(CurveParameters.Count);
                foreach (CurveParameter value in CurveParameters.Values)
                {
                    value.SaveToStream(streamEx);
                }
                list3.Add(streamEx.ToArray());
                streamEx.Close();
            }
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list2, list3);
            stream.Flush();
        }

        public bool ReadFromStream(StreamEx stream, List<Well> allWell)
        {
            List<Project> list = (from s in allWell
                                  where s.Project != null
                                  select s.Project).Distinct().ToList();
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_Subset)
            {
                propertyKey = stream.ReadKey();
                if (propertyKey == PropertyKey.Key_Index)
                {
                    List<PropertyKey> keys;
                    List<byte[]> dates;
                    stream.ReadIndex(out keys, out dates);
                    for (int i = 0; i < keys.Count; i++)
                    {
                        switch (keys[i])
                        {
                            case PropertyKey.Key_Name:
                                Name = BytesConverter.BytesToString(dates[i]);
                                break;
                            case PropertyKey.Key_Wells:
                                {
                                    StreamEx streamEx = new StreamEx();
                                    streamEx.Write(dates[i]);
                                    streamEx.Seek(0L, SeekOrigin.Begin);
                                    int num = streamEx.ReadInt();
                                    for (int k = 0; k < num; k++)
                                    {
                                        Wells.Add(allWell[streamEx.ReadInt()]);
                                    }
                                    streamEx.Close();
                                    break;
                                }
                            case PropertyKey.Key_SubsetParamters:
                                {
                                    StreamEx streamEx = new StreamEx();
                                    streamEx.Write(dates[i]);
                                    streamEx.Seek(0L, SeekOrigin.Begin);
                                    int num = streamEx.ReadInt();
                                    for (int l = 0; l < num; l++)
                                    {
                                        Project project = list[streamEx.ReadInt()];
                                        SubsetParamter subsetParamter = new SubsetParamter();
                                        ReadFromStreamSubsetParamter(subsetParamter, streamEx, project.BasicInfo.Type);
                                        SubsetParamters.Add(project, subsetParamter);
                                    }
                                    streamEx.Close();
                                    break;
                                }
                            case PropertyKey.Key_CurveParameters:
                                {
                                    StreamEx streamEx = new StreamEx();
                                    streamEx.Write(dates[i]);
                                    streamEx.Seek(0L, SeekOrigin.Begin);
                                    int num = streamEx.ReadInt();
                                    for (int j = 0; j < num; j++)
                                    {
                                        CurveParameter curveParameter = new CurveParameter();
                                        curveParameter.ReadFromStream(streamEx);
                                        CurveParameters.Add(curveParameter.WellNo.ToString("00") + curveParameter.ChannelNo, curveParameter);
                                    }
                                    streamEx.Close();
                                    break;
                                }
                        }
                    }
                    return true;
                }
                stream.Seek(-4L, SeekOrigin.Current);
                while (true)
                {
                    switch (stream.ReadKey())
                    {
                        case PropertyKey.Key_Name:
                            Name = stream.ReadString();
                            break;
                        case PropertyKey.Key_AMPAlgorithm:
                            stream.ReadInt();
                            break;
                        case PropertyKey.Key_Wells:
                            {
                                int num2 = stream.ReadInt();
                                for (int num3 = 0; num3 < num2; num3++)
                                {
                                    Wells.Add(allWell[stream.ReadInt()]);
                                }
                                break;
                            }
                        case PropertyKey.Key_SubsetParamters:
                            {
                                int num2 = stream.ReadInt();
                                for (int n = 0; n < num2; n++)
                                {
                                    Project project2 = list[stream.ReadInt()];
                                    SubsetParamter subsetParamter2 = new SubsetParamter();
                                    ReadFromStreamSubsetParamter(subsetParamter2, stream, project2.BasicInfo.Type);
                                    SubsetParamters.Add(project2, subsetParamter2);
                                }
                                break;
                            }
                        case PropertyKey.Key_GenoTypeList:
                            {
                                GenoTypeList.Clear();
                                int num2 = stream.ReadInt();
                                for (int m = 0; m < num2; m++)
                                {
                                    KeyValuePair keyValuePair = new KeyValuePair();
                                    keyValuePair.Key = (EGenoType)stream.ReadInt();
                                    keyValuePair.Value = stream.ReadString();
                                    GenoTypeList.Add(keyValuePair);
                                }
                                break;
                            }
                        default:
                            stream.Seek(-4L, SeekOrigin.Current);
                            return true;
                    }
                }
            }
            stream.Seek(-4L, SeekOrigin.Current);
            return false;
        }

        public void SaveToStreamSubsetParamter(SubsetParamter param, StreamEx stream)
        {
            stream.Write(PropertyKey.Key_SubsetParamter);
            List<PropertyKey> list = new List<PropertyKey>();
            List<byte[]> list2 = new List<byte[]>();
            StreamEx streamEx = null;
            if (param.BasicOption != null)
            {
                list.Add(PropertyKey.Key_ProjectBasicOption);
                streamEx = new StreamEx();
                param.BasicOption.SaveToStream(streamEx);
                list2.Add(streamEx.ToArray());
                streamEx.Close();
            }
            if (param.CrossTalk != null)
            {
                list.Add(PropertyKey.Key_ProjectCrossTalk);
                streamEx = new StreamEx();
                param.CrossTalk.SaveToStream(streamEx);
                list2.Add(streamEx.ToArray());
                streamEx.Close();
            }
            if (param.Rule != null)
            {
                list.Add(PropertyKey.Key_ProjectRule);
                streamEx = new StreamEx();
                param.Rule.SaveToStream(streamEx);
                list2.Add(streamEx.ToArray());
                streamEx.Close();
            }
            if (param.CurrentProjectStdCurve != null)
            {
                list.Add(PropertyKey.Key_ProjectStdCurve);
                streamEx = new StreamEx();
                param.CurrentProjectStdCurve.SaveToStream(streamEx);
                list2.Add(streamEx.ToArray());
                streamEx.Close();
            }
            if (param.RefTarget.Count > 0)
            {
                List<PropertyKey> list3 = new List<PropertyKey>();
                List<byte[]> list4 = new List<byte[]>();
                list.Add(PropertyKey.Key_RefTargets);
                streamEx = new StreamEx();
                streamEx.Write(param.RefTarget.Count);
                foreach (HRMRefTarget refTarget in param.RefTarget)
                {
                    list3.Clear();
                    list4.Clear();
                    streamEx.Write(PropertyKey.Key_RefTarget);
                    list3.Add(PropertyKey.Key_TargetName);
                    list4.Add(BytesConverter.GetBytes(refTarget.TargetName));
                    list3.Add(PropertyKey.Key_RefMode);
                    list4.Add(BytesConverter.GetBytes((int)refTarget.RefMode));
                    if (refTarget.AutoItem != null)
                    {
                        Well well = Wells.Where((Well s) => s.Targets.Contains(refTarget.AutoItem)).FirstOrDefault();
                        if (well != null)
                        {
                            list3.Add(PropertyKey.Key_AutoItem);
                            list4.Add(BytesConverter.GetBytes(well.CellName));
                        }
                    }
                    if (refTarget.ManualItem != null)
                    {
                        Well well2 = Wells.Where((Well s) => s.Targets.Contains(refTarget.ManualItem)).FirstOrDefault();
                        if (well2 != null)
                        {
                            list3.Add(PropertyKey.Key_ManualItem);
                            list4.Add(BytesConverter.GetBytes(well2.CellName));
                        }
                    }
                    streamEx.WriteIndex(list3, list4);
                }
                list2.Add(streamEx.ToArray());
                streamEx.Close();
            }
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list, list2);
            stream.Flush();
        }

        public bool ReadFromStreamSubsetParamter(SubsetParamter param, StreamEx stream, EProjectType type)
        {
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_SubsetParamter)
            {
                propertyKey = stream.ReadKey();
                if (propertyKey == PropertyKey.Key_Index)
                {
                    StreamEx streamEx = null;
                    List<PropertyKey> keys;
                    List<byte[]> dates;
                    stream.ReadIndex(out keys, out dates);
                    for (int i = 0; i < keys.Count; i++)
                    {
                        streamEx = new StreamEx();
                        streamEx.Write(dates[i]);
                        streamEx.Seek(0L, SeekOrigin.Begin);
                        switch (keys[i])
                        {
                            case PropertyKey.Key_ProjectBasicOption:
                                param.BasicOption = OptionFactory.CreateBasicOption(type);
                                param.BasicOption.ReadFromStream(streamEx);
                                break;
                            case PropertyKey.Key_ProjectCrossTalk:
                                param.CrossTalk = new CrossTalk();
                                param.CrossTalk.ReadFromStream(streamEx);
                                break;
                            case PropertyKey.Key_ProjectRule:
                                param.Rule = RuleFactory.CreateRule(type);
                                param.Rule.ReadFromStream(streamEx);
                                break;
                            case PropertyKey.Key_ProjectStdCurve:
                                param.CurrentProjectStdCurve = new ProjectStdCurve();
                                param.CurrentProjectStdCurve.ReadFromStream(streamEx);
                                break;
                            case PropertyKey.Key_RefTargets:
                                {
                                    int num = streamEx.ReadInt();
                                    for (int j = 0; j < num; j++)
                                    {
                                        propertyKey = streamEx.ReadKey();
                                        if (propertyKey == PropertyKey.Key_RefTarget)
                                        {
                                            HRMRefTarget refTarget = new HRMRefTarget();
                                            List<PropertyKey> keys2;
                                            List<byte[]> dates2;
                                            streamEx.ReadIndex(out keys2, out dates2);
                                            for (int k = 0; k < keys2.Count; k++)
                                            {
                                                string cellName;
                                                switch (keys2[k])
                                                {
                                                    case PropertyKey.Key_TargetName:
                                                        refTarget.TargetName = BytesConverter.BytesToString(dates2[k]);
                                                        break;
                                                    case PropertyKey.Key_RefMode:
                                                        refTarget.RefMode = (ERefMode)BytesConverter.BytesToInt(dates2[k]);
                                                        break;
                                                    case PropertyKey.Key_AutoItem:
                                                        cellName = BytesConverter.BytesToString(dates2[k]);
                                                        refTarget.AutoItem = Wells.Where((Well s) => s.CellName == cellName).FirstOrDefault().Targets.Where((SampleTargetItem s) => s.TargetName == refTarget.TargetName).FirstOrDefault();
                                                        break;
                                                    case PropertyKey.Key_ManualItem:
                                                        cellName = BytesConverter.BytesToString(dates2[k]);
                                                        refTarget.ManualItem = Wells.Where((Well s) => s.CellName == cellName).FirstOrDefault().Targets.Where((SampleTargetItem s) => s.TargetName == refTarget.TargetName).FirstOrDefault();
                                                        break;
                                                }
                                            }
                                            param.RefTarget.Add(refTarget);
                                            continue;
                                        }
                                        streamEx.Close();
                                        return false;
                                    }
                                    break;
                                }
                        }
                        streamEx.Close();
                    }
                    return true;
                }
                stream.Seek(-4L, SeekOrigin.Current);
                while (true)
                {
                    switch (stream.ReadKey())
                    {
                        case PropertyKey.Key_ProjectBasicOption:
                            param.BasicOption = OptionFactory.CreateBasicOption(type);
                            param.BasicOption.ReadFromStream(stream);
                            break;
                        case PropertyKey.Key_ProjectCrossTalk:
                            param.CrossTalk = new CrossTalk();
                            param.CrossTalk.ReadFromStream(stream);
                            break;
                        case PropertyKey.Key_ProjectRule:
                            param.Rule = RuleFactory.CreateRule(type);
                            param.Rule.ReadFromStream(stream);
                            break;
                        case PropertyKey.Key_ProjectStdCurve:
                            param.CurrentProjectStdCurve = new ProjectStdCurve();
                            param.CurrentProjectStdCurve.ReadFromStream(stream);
                            break;
                        case PropertyKey.Key_RefTargets:
                            {
                                int num2 = stream.ReadInt();
                                for (int l = 0; l < num2; l++)
                                {
                                    propertyKey = stream.ReadKey();
                                    if (propertyKey == PropertyKey.Key_RefTarget)
                                    {
                                        bool flag = false;
                                        HRMRefTarget refTarget2 = new HRMRefTarget();
                                        while (!flag)
                                        {
                                            propertyKey = stream.ReadKey();
                                            string cellName2;
                                            switch (propertyKey)
                                            {
                                                case PropertyKey.Key_TargetName:
                                                    refTarget2.TargetName = stream.ReadString();
                                                    break;
                                                case PropertyKey.Key_RefMode:
                                                    refTarget2.RefMode = (ERefMode)stream.ReadInt();
                                                    break;
                                                case PropertyKey.Key_AutoItem:
                                                    cellName2 = stream.ReadString();
                                                    refTarget2.AutoItem = Wells.Where((Well s) => s.CellName == cellName2).FirstOrDefault().Targets.Where((SampleTargetItem s) => s.TargetName == refTarget2.TargetName).FirstOrDefault();
                                                    break;
                                                case PropertyKey.Key_ManualItem:
                                                    cellName2 = stream.ReadString();
                                                    refTarget2.ManualItem = Wells.Where((Well s) => s.CellName == cellName2).FirstOrDefault().Targets.Where((SampleTargetItem s) => s.TargetName == refTarget2.TargetName).FirstOrDefault();
                                                    break;
                                                default:
                                                    stream.Seek(-4L, SeekOrigin.Current);
                                                    flag = true;
                                                    break;
                                            }
                                        }
                                        param.RefTarget.Add(refTarget2);
                                    }
                                    else
                                    {
                                        stream.Seek(-4L, SeekOrigin.Current);
                                    }
                                }
                                break;
                            }
                        default:
                            stream.Seek(-4L, SeekOrigin.Current);
                            return true;
                    }
                }
            }
            stream.Seek(-4L, SeekOrigin.Current);
            return false;
        }
    }
}
